﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using YLServerInterface.UserControls;

namespace WpfControlLibrary
{
    /// <summary>
    /// Interaction logic for WrapPanelChangePosition.xaml
    /// </summary>
    public partial class WrapPanelChangePosition : UserControl
    {
        public WrapPanelChangePosition()
        {
            InitializeComponent();
        }
        public List<ImageCutter> Items = new List<ImageCutter>();
        public void AddElement(ImageCutter e)
        {
            Items.Add(e);
            e.Tag = Items.Count - 1;
            wp.Children.Add(e);
            e.MoveLeft = btnMoveLeft_Click;
            e.MoveRight = btnMoveRight_Click;
          //  e.PreviewMouseMove += new MouseEventHandler(e_PreviewMouseMove);
           
        }
        AdornerLayer mAdornerLayer = null;
        void e_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            DragDropAdorner adorner = new DragDropAdorner(sender as ImageCutter);
            mAdornerLayer = AdornerLayer.GetAdornerLayer(grid); // Window class do not have AdornerLayer
            mAdornerLayer.Add(adorner);


            DataObject dataObject = new DataObject(sender as ImageCutter);
            // Here, we should notice that dragsource param will specify on which 
            // control the drag&drop event will be fired
            System.Windows.DragDrop.DoDragDrop(sender as ImageCutter, dataObject, DragDropEffects.Copy);


            mAdornerLayer.Remove(adorner);
            mAdornerLayer = null;
        }
        public void RemoveElements()
        {
            Items.Clear();
            wp.Children.Clear();
        }
        /// <summary>
        /// 交换imgCutter逻辑：
        /// 图片保存名字要交换 其余不交换
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        private void swap(int a, int b)
        {
            Items[a].SwapConfig(Items[b]);
            var t = Items[a];
            Items[a] = Items[b];
            Items[b] = t;
        }
        void resetWp()
        {
            wp.Children.Clear();
            foreach (var item in Items)
            {
                wp.Children.Add(item);
            }
        }

        private void btnMoveLeft_Click(ImageCutter cutter)
        {
            var curI = wp.Children.IndexOf(cutter);
            if (curI - 1 >= 0)
            {
                swap(curI, curI - 1);
            }
            resetWp();
        }

        private void btnMoveRight_Click(ImageCutter cutter)
        {
            var curI = wp.Children.IndexOf(cutter);
            if (curI + 1 < Items.Count)
            {
                swap(curI, curI + 1);
            }
            resetWp();
        }
    }
}
